package asteroids.model.programs.binaryexpression;

import asteroids.model.programs.ComposedExpression;
import asteroids.model.programs.Expression;
import be.kuleuven.cs.som.annotate.*;
import exception.IllegalOperandException;

@SuppressWarnings("all")
public abstract class BinaryExpression<T> extends ComposedExpression<T> {

	@Model
	protected BinaryExpression(Expression left, Expression right)
			throws IllegalOperandException {
		if (!canHaveAsOperandAt(left,1))
			throw new IllegalOperandException(this, left);
		if (!canHaveAsOperandAt(right,2))
			throw new IllegalOperandException(this, right);
		setOperandAt(1, left);
		setOperandAt(2, right);
		
	}


	@Override
	@Basic @Immutable
	public final int getNbOperands() {
		return 2;
	}

	@Override
	@Raw
	public final Expression<T> getOperandAt(int index)
			throws IndexOutOfBoundsException {
		if ((index != 1) && (index != 2))
			throw new IndexOutOfBoundsException();
		if (index == 1)
			return getLeftOperand();
		else
			return getRightOperand();
	}
	

	@Override
	public boolean canHaveAsOperandAt(Expression expression, int index) {
		return super.canHaveAsOperandAt(expression, index) && ( (index == 1) || (index == 2) );
	}

	
	@Override
	@Raw
	protected void setOperandAt(int index, Expression operand) {
		if (index == 1)
			this.leftOperand = operand;
		else
			this.rightOperand = operand;
	}


	@Basic
	public Expression getLeftOperand() {
		return leftOperand;
	}


	private Expression leftOperand;

	
	@Basic
	public Expression getRightOperand() {
		return rightOperand;
	}

	
	private Expression rightOperand;
	



}
